﻿using MeetingAssistant.CoreBLL.Service.UploadExcel.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MeetingAssistant.ViewModel.Member;
using System.Data.Entity.Validation;
using System.IO;
using LinqToExcel;

namespace MeetingAssistant.CoreBLL.Service.UploadExcel
{
    public partial class CheckExcelData : ICheckExcelData
    {
        /// <summary>
        /// 校验数据
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="userList"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public bool CheckUploadData(string fileName, List<UserVM> userList, out string error)
        {
            var targetFile = new FileInfo(fileName);
            if (!targetFile.Exists)
            {
                error = "该文件不存在！请重新上传！";
                return false;
            }

            var excel = new ExcelQueryFactory(fileName);
            var workSheetNames = excel.GetWorksheetNames();
            //对应
            foreach (var item in workSheetNames)
            {
                if (item.Contains("用户表"))
                {
                    var columnNames = excel.GetColumnNames(item);
                    excel.AddMapping<UserVM>(d => d.Id, "序号");
                    excel.AddMapping<UserVM>(d => d.UserName, "用户名");
                    excel.AddMapping<UserVM>(d => d.TrueName, "姓名");
                    excel.AddMapping<UserVM>(d => d.Sex, "性别");
                    excel.AddMapping<UserVM>(d => d.IdentityNumber, "身份证号");
                    excel.AddMapping<UserVM>(d => d.Email, "邮箱");
                    excel.AddMapping<UserVM>(d => d.Phone, "电话");
                    excel.AddMapping<UserVM>(d => d.Address, "个人住址");
                    excel.AddMapping<UserVM>(d => d.Department, "所属部门");

                    var tsheet = excel.Worksheet<UserVM>(item);
                    var rowIndex = 1;

                    foreach(var row in tsheet)
                    {
                        var user = new UserVM();

                        user.UserName = row.UserName;
                        user.TrueName = row.TrueName;
                        user.Sex = row.Sex;
                        user.IdentityNumber = row.IdentityNumber;
                        user.Email = row.Email;
                        user.Phone = row.Phone;
                        user.Address = row.Address;
                        user.Department = row.Department;

                        if (string.IsNullOrWhiteSpace(row.UserName)
                            || string.IsNullOrWhiteSpace(row.TrueName)
                            || string.IsNullOrWhiteSpace(row.Sex.ToString())
                            || string.IsNullOrWhiteSpace(row.IdentityNumber)
                            || string.IsNullOrWhiteSpace(row.Email)
                            || string.IsNullOrWhiteSpace(row.Phone)
                            || string.IsNullOrWhiteSpace(row.Address)
                            || string.IsNullOrWhiteSpace(row.Department)
                            || string.IsNullOrWhiteSpace(row.IdentityNumber))
                        {
                            error = string.Format($"第{rowIndex}列发现错误！");
                            return false;
                        }
                        userList.Add(user);
                        rowIndex += 1;
                    }
                }
            }
            error = null;
            return true;
        }
        
    }
}
